﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MP3FolderScrubber
{
    public partial class frmMain : Form
    {
        private string[] _subDirs;
        private string _results;
        bool _deleteFolders;

        public frmMain()
        {
            InitializeComponent();
            pnlInfo.Visible = false;
            prgBar.Visible = false;

            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.DoWork += backgroundWorker_DoWork;
            backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            fbdRoot.ShowDialog();
            txtFolder.Text = fbdRoot.SelectedPath;
        }

        private void btnAnalyze_Click(object sender, EventArgs e)
        {
            prgBar.Visible = true;
            prgBar.Value = 0;
            btnAnalyze.Enabled = false;
            btnScrub.Enabled = false;
            btnBrowse.Enabled = false;
            pnlInfo.Visible = false;
            txtFolder.Enabled = false;
            _deleteFolders = false;
            backgroundWorker.RunWorkerAsync();
        }

        private void btnScrub_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure?  This will delete any folders shown during the Analyze pass and any files underneath.", "Confirm Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result != DialogResult.OK)
                return;

            prgBar.Visible = true;
            prgBar.Value = 0;
            btnAnalyze.Enabled = false;
            btnScrub.Enabled = false;
            btnBrowse.Enabled = false;
            pnlInfo.Visible = false;
            txtFolder.Enabled = false;
            _deleteFolders = true;
            backgroundWorker.RunWorkerAsync();
        }

        private void SearchFolders(string rootFolder)
        {
            _subDirs = new string[0];

            try
            {
                _subDirs = CheckFolderRecursive(rootFolder);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            StringBuilder sBuilder = new StringBuilder();

            for(int i=0; i<_subDirs.Length; i++)
            {
                sBuilder.Append(_subDirs[i]);
                sBuilder.Append("\r\n");

                try
                {
                    if (_deleteFolders)
                        Directory.Delete(_subDirs[i], true);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            _results = sBuilder.ToString();
        }

        private string[] CheckFolderRecursive(string folder)
        {
            string[] directories = Directory.GetDirectories(folder);
            string[] files = Directory.GetFiles(folder, "*.mp3");
            string[] returnDirs = new string[0];

            if (directories.Length == 0 && files.Length == 0)
                return new string[] { folder };

            foreach(string directory in directories)
            {
                string[] subDirectoriesToDelete = CheckFolderRecursive(directory);
                returnDirs = MergeArray(returnDirs, subDirectoriesToDelete);
            }

            return returnDirs;
        }

        private string[] MergeArray(string[] a, string[] b)
        {
            string[] c = new string[a.Length + b.Length];
            a.CopyTo(c, 0);
            b.CopyTo(c, a.Length);

            return c;
        }

        #region Threading
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            SearchFolders(txtFolder.Text);
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            prgBar.Visible = false;
            btnAnalyze.Enabled = true;
            btnScrub.Enabled = true;
            btnBrowse.Enabled = true;
            pnlInfo.Visible = true;
            txtFolder.Enabled = true;
            lblFolderCount.Text = _subDirs.Length.ToString();
            txtResults.Text = _results;            
        }

        #endregion

        
    }
}
